package com.funsport.controller.mobile;

import com.funsport.common.Result;
import com.funsport.entity.FriendRequest;
import com.funsport.entity.User;
import com.funsport.security.SecurityUtils;
import com.funsport.service.IFriendService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 移动端好友控制器
 */
@Slf4j
@Tag(name = "移动端好友管理")
@RestController
@RequestMapping("/mobile/friend")
@RequiredArgsConstructor
public class MobileFriendController {
    
    private final IFriendService friendService;
    
    @Operation(summary = "获取好友列表")
    @GetMapping("/list")
    public Result<List<User>> getFriendList() {
        Long userId = SecurityUtils.getCurrentUserId();
        List<User> friends = friendService.getFriendList(userId);
        return Result.success(friends);
    }
    
    @Operation(summary = "发送好友申请")
    @PostMapping("/request")
    public Result<Void> sendFriendRequest(@RequestParam Long toUserId, 
                                          @RequestParam(required = false) String message) {
        Long fromUserId = SecurityUtils.getCurrentUserId();
        
        if (fromUserId.equals(toUserId)) {
            return Result.error("不能添加自己为好友");
        }
        
        boolean success = friendService.sendFriendRequest(fromUserId, toUserId, message);
        return success ? Result.success() : Result.error("发送失败，可能已是好友或已发送过申请");
    }
    
    @Operation(summary = "获取好友申请列表")
    @GetMapping("/requests")
    public Result<List<FriendRequest>> getFriendRequests() {
        Long userId = SecurityUtils.getCurrentUserId();
        List<FriendRequest> requests = friendService.getFriendRequests(userId);
        return Result.success(requests);
    }
    
    @Operation(summary = "接受好友申请")
    @PostMapping("/accept/{requestId}")
    public Result<Void> acceptFriendRequest(@PathVariable Long requestId) {
        Long userId = SecurityUtils.getCurrentUserId();
        boolean success = friendService.acceptFriendRequest(requestId, userId);
        return success ? Result.success() : Result.error("接受失败");
    }
    
    @Operation(summary = "拒绝好友申请")
    @PostMapping("/reject/{requestId}")
    public Result<Void> rejectFriendRequest(@PathVariable Long requestId) {
        Long userId = SecurityUtils.getCurrentUserId();
        boolean success = friendService.rejectFriendRequest(requestId, userId);
        return success ? Result.success() : Result.error("拒绝失败");
    }
    
    @Operation(summary = "删除好友")
    @DeleteMapping("/{friendId}")
    public Result<Void> deleteFriend(@PathVariable Long friendId) {
        Long userId = SecurityUtils.getCurrentUserId();
        boolean success = friendService.deleteFriend(userId, friendId);
        return success ? Result.success() : Result.error("删除失败");
    }
    
    @Operation(summary = "检查是否是好友")
    @GetMapping("/check/{friendId}")
    public Result<Boolean> checkFriend(@PathVariable Long friendId) {
        Long userId = SecurityUtils.getCurrentUserId();
        boolean isFriend = friendService.isFriend(userId, friendId);
        return Result.success(isFriend);
    }
    
    @Operation(summary = "搜索用户")
    @GetMapping("/search")
    public Result<List<User>> searchUsers(@RequestParam String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            return Result.success(Collections.emptyList());
        }
        
        List<User> users = friendService.searchUsers(keyword.trim());
        return Result.success(users);
    }
}

